<?php
namespace BoxSns\Controller\Home;
//use Think\Controller;
class UserController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$this->nav_cur = "Home";
		//	if(!$this->user_info['user_id']){
		//			Logic("BoxSns","Member")->toLogin();
		//		}
		//		$this->this_user_id=$user_id=I('user_id',$this->user_id,"intval");
		//		$this->member_info=BoxModel("system_member")->load($user_id);
		$this->webName = "";
		$this->title = $this->member_info['nickname'];
	}

	public function index() {
		$this->title = $this->member_info['nickname'];
		$ta = array(
			'1' => '他',
			'2' => '她',
		);
		$this->TA = $ta[$this->member_info['sex']];
		$this->TA = $this->TA ? $this->TA : "TA";
		$this->user_statistics = Logic("BoxSns", "Member")->statistics(array('user_id' => $this->this_user_id));
		$this->show_user();
	}

	/******帖子**/
	public function topic() {
		$where = "store_id='$this->store_id'";
		$this->group_id = $group_id = I('group_id', "0", "intval");
		if ($user_id = I('user_id')) {
			$where .= " and user_id=$user_id";
		}
		$this->where = $where;
		$this->show_user();
	}

	/******我的粉丝**/
	public function fans() {
		$this->title = "粉丝列表";
		$data = Logic("BoxSns", "Friends")->my_fans(array('user_id' => $this->this_user_id));
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->show_user("User/follow");
	}

	/******我的关注**/
	public function follow() {
		$this->title = "关注列表";
		$data = Logic("BoxSns", "Friends")->my_follow(array('user_id' => $this->this_user_id, 'limit' => 10));
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->show_user("User/follow");
	}

	/******我的资料**/
	public function info() {
		//  $this->title = "个人信息";
		$this->web_title = "帐号设置 - $this->webName";
		$this->this_user_id = I('user_id', $this->user_info['user_id'], "intval");
		$this->data = BoxModel("system_member")->load($this->this_user_id);
		//	dump(  $this->data);
		if ($this->this_user_id != $this->user_info['user_id'] && I('user_id')) {
			$tpl = "User/info";
		} else {
			$tpl = "User/info_edit";
		}

		$this->show_user($tpl);
	}

	//保存个人信息
	public function saveInfo() {
		$model = BoxModel("system_member");
		$row = array(
			'nickname' => I('nickname'),
			'province_id' => I('province_id'),
			'name' => I('name'),
			'birthday' => I('birthday'),
			'user_id' => $this->user_info['user_id'],
		);
		$row['type'] = "students";
		$model->create_member($row);
		$this->jump("保存成功");
	}

	//保存地址
	public function saveAddress() {
		$model = BoxModel("system_member");
		$row = array(
			'address' => I('address'),
			'user_id' => $this->user_info['user_id'],
		);
		$model->create_member($row);
		$this->jump("保存成功");
	}

	//获得手机验证码
	public function send_code() {
		$code = rand(100000, 999999);
		$tel = I('phone');
		if (!preg_match('/^1\d{10}$/', $tel)) {
			exit(json_encode(array('error' => 1, 'msg' => '手机号必须是1开头的11位数字')));
		}
		$r = BoxModel("SystemMember")->ByField($tel, "phone");
		if ($r) {
			exit(json_encode(array('error' => 3, 'msg' => '手机号已经被占用，请更换其它手机号')));
		}
		/*******发送短信******/
		$r = sms($tel, "验证码：" . $code . "【盛明体育科技】");
		session("tel_code", $code);
		exit(json_encode(array('error' => 0, 'msg' => $code)));
		if ($r === true) {
			session("tel_code", $code);
			exit(json_encode(array('error' => 0, 'msg' => "验证码已经发送成功，请注意查收")));
		} else {
			exit(json_encode(array('error' => 1, 'msg' => $r)));
		}
	}

	public function bing_phone() {
		$this->show_user();
	}

	//绑定手机号
	public function bingPhone() {
		$model = BoxModel("SystemMember");
		$code = I('txtPhoneCode');
		$tel = I('phone');
		if (!preg_match('/^1\d{10}$/', $tel)) {
			exit(json_encode(array('error' => 1, 'msg' => '手机号必须是1开头的11位数字')));
		}
		if ($code != session('tel_code')) {
			exit(json_encode(array('error' => 4, 'msg' => '手机验证码不正确')));
		}
		$r = $model->ByField($tel, "phone");
		if ($r) {
			exit(json_encode(array('error' => 3, 'msg' => '手机号已经存在')));
		}
		$row = array(
			'phone' => $tel,
			'user_id' => $this->user_info['user_id'],
			'mobile' => $tel,
			'type' => 'students',
		);
		$user_id = $model->create_member($row);
		if ($user_id) {
			session("tel_code", NULL);
			exit(json_encode(array('error' => 0, 'msg' => '手机号保存成功')));
		} else {
			exit(json_encode(array('error' => 1, 'msg' => '手机号保存失败，请重试')));
		}
	}

	/******我的提醒**/
	public function noti() {
		$this->web_title = "消息提醒 - $this->webName";
		$data = Logic("BoxSns", "Noti")->get();
		BoxModel("SnsNotificationCount")->clear($this->user_id, "comment");
		foreach ($data['list'] as $k => $one) {
			$data['list'][$k]['time_tran'] = time_tran($one['addtime']);
		}
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$ids = array();
		foreach ($data['list'] as $one) {
			$ids[] = $one['id'];
		}
		$ids = implode(",", $ids);
		$this->ids = $ids;
		$this->show_user();
	}

	public function clearNoti() {
		$ids = I('ids');
		if ($ids) {
			BoxModel("sns_notification")->where(" id in ($ids)")->save(array('state' => 1));
		}
		exit();
	}

	/******我的消息**/
	public function message() {
		$data = Logic("BoxSns", "msg")->message($p);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->show_user();
	}

	/******我的消息**/
	public function message_show() {
		$to_user_id = I('to_user_id');
		$this->to_member_info = BoxModel("system_member")->load($to_user_id);
		$this->box_id = $box_id = ($to_user_id > $this->user_info['user_id']) ? ($this->user_info['user_id'] . "_" . $to_user_id) : ($to_user_id . "_" . $this->user_info['user_id']);
		$data = Logic("BoxSns", "msg")->show_message(array('to_user_id' => $to_user_id));
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->show_user();
	}

	/***我的钱包**/
	public function money() {
		$this->web_title = "我的财务 - $this->webName";
		$this->type = I('type', "all");
		$where['type'] = 'money';
		$where['user_id'] = $this->user_info['user_id'];
		if ($this->type == "in") {
			$where['num'] = array('gt', "0");
		}
		if ($this->type == "out") {
			$where['num'] = array('lt', "0");
		}
		if ($this->start_time = I('start_time')) {
			$where['add_date'][] = array('gt', $this->start_time);
		}
		if ($this->end_time = I('end_time')) {
			$where['add_date'][] = array('lt', $this->end_time);
		}
		$this->where = $where;
		$this->Sum = BoxModel("system_log")->where($where)->sum("num");
		$this->Sum = (int) ($this->Sum ? $this->Sum : 0);
		$this->show_user();
	}

	/***我要充值**/
	public function recharge() {
		$this->show_user();
	}

	public function do_recharge() {
		$row = array(
			'money' => I('money'),
			// 'payment_id' => '43',
		);
		// if (!checkAgent()) {
		//            $row['payment_id'] = 44;
		//        }
		$return = Logic("BoxSns", "Member")->recharge($row);
		exit(json_encode($return));
	}

	public function withdraw() {
		$this->show_user();
	}

	/***我要提现**/
	public function doWithdraw() {
		// if (!$this->user_info['phone']) {
		//            $this->jump("请先绑定手机，再提现", U('bing_phone'));
		//        }
		if (IS_AJAX) {
			$code = I('code');
			$tel = I('tel');
			$money = I('money');
			$account = I('account');
			$account_type = I('account_type');
			if (!preg_match('/^1\d{10}$/', $tel)) {
				exit(json_encode(array('error' => 1, 'msg' => '手机号必须是1开头的11位数字')));
			}
			if ($code != session('tel_code')) {
				exit(json_encode(array('error' => 4, 'msg' => '手机验证码不正确')));
			}
			if (!$account || !$account_type) {
				exit(json_encode(array('error' => 7, 'msg' => '请输入提现到帐户')));
			}
			if ($money < 50) {
				exit(json_encode(array('error' => 7, 'msg' => '最少提现50元')));
			}
			if (intval($money) == 0) {
				exit(json_encode(array('error' => 5, 'msg' => '请输入大于0的金额')));
			}
			if ($money > $this->user_info['money']) {
				exit(json_encode(array('error' => 6, 'msg' => '提现金额不能大于帐户余额')));
			}

			$row = array(
				'user_id' => $this->user_info['user_id'],
				'money' => $money,
				'addtime' => date("Y-m-d H:i:s"),
				'account' => $account,
				'phone' => $tel,
				'account_type' => $account_type,
			);
			//$money=$this->user_info['money']-$money;
			//	$r2=BoxModel("system_member")->where(array('user_id'=>$this->user_info['user_id']))->save(array('money'=>$money));
			/******提现申请*****************************/

			$logRow = array(
				'user_id' => $this->user_info['user_id'],
				'num' => -$money,
				'order_sn' => "",
				'event' => "00007",
				'subjectid' => 0,
			);
			$r2 = Logic('BoxSns', 'Log')->add($logRow);
			if ($r2) {
				$r = BoxModel("system_money_withdraw")->add($row);
			}
			if ($r) {
				exit(json_encode(array('error' => 0, 'msg' => '提交成功！')));
			} else {
				exit(json_encode(array('error' => 8, 'msg' => '数据处理失败，请重试')));
			}
		}
		$this->show_user();
	}

	/*******提现列表****/
	public function withdraw_list() {
		$this->status_array = array(
			'1' => '提现中',
			'2' => '提现成功',
			'3' => '驳回',
		);
		$user_id = $this->user_info['user_id'];
		$data = BoxModel("SystemMoneyWithdraw")->get("user_id='$user_id'", 10);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->show_user();
	}

	function withdraw_set() {
		$data = BoxModel("system_withdraw_setting")->where(array('user_id' => $this->user_info['user_id']))->find();
		$this->setting = $setting = (array) json_decode($data['setting'], true);
		if (IS_POST) {
			$setting = I('setting');
			$account = trim($setting['account']);
			$idCard = trim($setting['idCard']);
			$code = I('txtPhoneCode');

			if (!$idCard) {
				exit(json_encode(array('error' => 1, 'msg' => '身份证不能为空')));
			}

			if (!$account) {
				exit(json_encode(array('error' => 1, 'msg' => '银行帐号不能为空')));
			}

			if ($code != session('tel_code')) {
				exit(json_encode(array('error' => 4, 'msg' => '手机验证码不正确')));
			}
			//验证卡
			$payClass = new \Common\Org\YzPay\YzPay();
			$param = array(
				'bankCardNum' => $setting['account'],
				'idCard' => $setting['idCard'],
				'idCardName' => $setting['name'],
			);
			$check = $payClass->checkBank($param);
			if ($check['responseCode'] != "0000") {
				exit(json_encode(array('error' => 3, 'msg' => $check['responseMsg'])));
			} else {
				$setting['bankName'] = $check['bankName'];
			}
			$setting['account'] = $account;
			$row = array('setting' => json_encode($setting));
			if ($data) {
				$r = BoxModel("system_withdraw_setting")->where(array('user_id' => $this->user_info['user_id']))->save($row);
			} else {
				$row['user_id'] = $this->user_info['user_id'];
				$r = BoxModel("system_withdraw_setting")->add($row);
			}

			if ($r) {
				exit(json_encode(array('error' => 0, 'msg' => '保存成功')));
			} else {
				exit(json_encode(array('error' => 2, 'msg' => '保存失败')));
			}
		}
		$this->display();
	}

}
